package com.wing.ext.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wing.common.CommonSpringJDBC;
import com.wing.common.util.PageBean;
import com.wing.ext.domain.Information;

@Repository
public class InformationDao extends CommonSpringJDBC<Information, String> {

	public PageBean findPageList(int currentPage, int pageSize, Object... args) {
		String where= " site_id='"+args[0]+"'  and (t.pid is null or t.pid='') ";
		String sql1=" SELECT * FROM ext_information where "+where+" ORDER BY id ASC " +
			" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(id) from  ext_information  where"+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public PageBean findInformationsByType(int currentPage, int pageSize,String site_id, String type_id,
			String toHome) {
		String where= " t.site_id='"+site_id+"'  and (t.pid is null or t.pid='')  ";
		if(type_id!=null){
			where += " and (t.type_id='"+type_id+"' or t.type_id2='"+type_id+"')  ";
		}
		if("1".equals(toHome)){
			where += " and t.toHome='1' ";
		}
		String sql1=" SELECT t.*,u.login_name FROM ext_information t,ext_site_user u where t.creator=u.id and "+where+" ORDER BY t.totop desc,t.createtime desc "+
				" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		
		String sql2 = "select count(t.id) from ext_information t,ext_site_user u where t.creator=u.id and "+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public List getSearchIndex(String site_id, String inforField, String type_id) {
		String where= " i.site_id='"+site_id+"'  and (i.pid is null or i.pid='')  ";
		if(type_id!=null && !type_id.trim().equals("")){
			where += " and i.type_id='"+type_id+"' ";
		}
		if(inforField!=null && !"".equals(inforField)){
			where += " and (i.title like '%"+inforField+"%' or i.content like '%"+inforField+"%') ";
		}
		String sql=" SELECT c.name,i.type_id2,count(i.id) as c FROM ext_information i,sys_code c where "+where+" and c.code = i.type_id2 group by i.type_id2 " ;
		return this.getSimpleJdbcTemplate().queryForList(sql);
	}

	public PageBean findInformationsByTypeField(int currentPage, int pageSize, String site_id, String inforField,String type_id,
			String type_id2) {
		String where= " site_id='"+site_id+"' and (pid is null or pid='') ";
		if(inforField!=null && !"".equals(inforField)){
			where += " and (title like '%"+inforField+"%' or content like '%"+inforField+"%') ";
		}
		if(type_id2!=null && !type_id2.trim().equals("") && !type_id2.trim().equals("null")){
			where += " and type_id2='"+type_id2+"' ";
		}
		if(type_id!=null && !type_id.trim().equals("") && !type_id.trim().equals("null")){
			where += " and type_id='"+type_id+"' ";
		}
		String sql1=" SELECT * FROM ext_information where "+where+" ORDER BY totop desc,createtime desc "+
				" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		String sql2 = "select count(id) from ext_information  where "+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public PageBean findHInformationsByType(int currentPage, int pageSize,
			String site_id, String type_id, String toHome) {
		String where= " t.site_id='"+site_id+"' and (t.pid is null or t.pid='') ";
		if(type_id!=null && !type_id.equals("")){
			where += " and (t.type_id='"+type_id+"' or t.type_id2='"+type_id+"')  ";
		}
		if("1".equals(toHome)){
			where += " and t.toHome='1' ";
		}
		String sql1=" SELECT t.* FROM ext_information t where "+where+" ORDER BY t.totop desc,t.createtime desc "+
				" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		
		String sql2 = "select count(t.id) from ext_information t where "+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public List<Map<String, Object>> findChildByID(String pid, String siteId) {
		String sql = "select t.* from ext_information t where t.pid=? and t.site_id=? order by t.totop desc,t.createtime desc";
		Object[] objs = { pid, siteId };
		return super.getSimpleJdbcTemplate().queryForList(sql, objs);
	}

	public PageBean getReceiptInformationsByPid(int currentPage, int pageSize,
			String site_id, String pid) {
		String where= " t.site_id='"+site_id+"'  and t.pid ='"+pid+"'  ";
		
		String sql1=" SELECT t.*,u.login_name FROM ext_information t,ext_site_user u where t.creator=u.id and "+where+" ORDER BY t.createtime "+
				" LIMIT "+(currentPage-1) * pageSize+","+pageSize;
		
		String sql2 = "select count(t.id) from ext_information t,ext_site_user u where t.creator=u.id and "+where;
		String[] sqlArray = {sql1,sql2 };
		return super.findPageList(sqlArray, currentPage, pageSize);
	}

	public List<Map<String, Object>> getChildByPid(String id) {
		String sql = "select t.* from ext_information t where t.pid=? ";
		Object[] objs = { id };
		return super.getSimpleJdbcTemplate().queryForList(sql, objs);
	}
	
	public void updateInformationContent(Information information){
		String sql = " UPDATE ext_information SET content = ?  where id = ?";
		Object [] objs = {information.getContent(),information.getId()};
		super.getSimpleJdbcTemplate().update(sql, objs);
	}
}
